//
//  AddcompanyInfoTableViewController.m
//  帮惠Mall
//
//  Created by reew on 2017/9/21.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "AddcompanyInfoTableViewController.h"
#import "User.h"
#import "CommonUtils.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import "AddCompanyTableViewController.h"
#import "System.h"
@interface AddcompanyInfoTableViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *companyName;
@property (weak, nonatomic) IBOutlet UITextField *mobilePhone;
@property (weak, nonatomic) IBOutlet UITextField *telephone;
@property (weak, nonatomic) IBOutlet UITextField *email;
@property (weak, nonatomic) IBOutlet UITextField *address;

@end

@implementation AddcompanyInfoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"完善信息";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(sendRequest)];
    right.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem  = right;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    //添加手势 点击页面隐藏键盘
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.tableView addGestureRecognizer:tapGestureRecognizer];
    tapGestureRecognizer.cancelsTouchesInView = NO;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];;
}
- (void)sendRequest{
    [CommonUtils showHudWithStatus:LoadingStatus stayTime:1 info:nil];
    if ([_companyName.text isEqualToString:@""]) {
        [SVProgressHUD setMaximumDismissTimeInterval:0.5];
        [SVProgressHUD showErrorWithStatus:@"公司名称不能为空"];
    }else{
        NSString *url = [kBaseURL stringByAppendingPathComponent:kAddCompany];
        AFHTTPSessionManager *manager = [CommonUtils getAFHTTPSessionManager];
        [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            //参数必选：adminNo（管理员编号）name（企业名称）
            //   可选：mobile（手机号）telephone（座机号）email（邮箱）address（地址）
            NSString *adminNo = [[NSUserDefaults standardUserDefaults] objectForKey:@"symbol"];
            if (!(adminNo == nil)) {
                [formData appendPartWithFormData:[adminNo dataUsingEncoding:NSUTF8StringEncoding] name:@"adminNo"];
            }
            [formData appendPartWithFormData:[_companyName.text dataUsingEncoding:NSUTF8StringEncoding] name:@"name"];
            [formData appendPartWithFormData:[_mobilePhone.text dataUsingEncoding:NSUTF8StringEncoding] name:@"mobile"];
            [formData appendPartWithFormData:[_telephone.text dataUsingEncoding:NSUTF8StringEncoding] name:@"telephone"];
            [formData appendPartWithFormData:[_email.text dataUsingEncoding:NSUTF8StringEncoding] name:@"email"];
            [formData appendPartWithFormData:[_address.text dataUsingEncoding:NSUTF8StringEncoding] name:@"address"];
            
        } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@",responseObject);
            NSNumber *result = responseObject[@"result"];
            NSString *message = responseObject[@"message"];
            
            if ([result integerValue] == 1) {
                //添加企业成功，保存companyNo
                NSString *companyNo = responseObject[@"companyNo"];
                [[NSUserDefaults standardUserDefaults] setObject:companyNo forKey:@"companyNo"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [SVProgressHUD setMaximumDismissTimeInterval:0.5];
                [SVProgressHUD showSuccessWithStatus:message];
                //跳转企业认证页面
                [self skipVC];
                
            }else{
                [CommonUtils showHudWithStatus:ErrorStatus stayTime:1 info:message]; 
            }
            //[self skipVC];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             //[self skipVC];
        }];
    }
}
- (void)skipVC{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    AddCompanyTableViewController *vc = [sb instantiateViewControllerWithIdentifier:@"companyAuth"];
    vc.title = @"企业认证";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)hideKeyBoard{
    [self.tableView endEditing:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
